<?php $weekending = strtotime($timesheet['Timesheet']['title']); ?>
<h2>Week Ending: <?php echo date('F j, Y',$weekending) ?></h2>

<?php
$taskbyclient = array();
foreach($tasks as $task):
	//$taskbyclient[$task['Task']['client_id']][$task['Task']['id']] = $task['Task'];
	$taskbyclient[$task['Task']['client_id']][$task['Task']['id']] = $task['Task'];
endforeach;
unset($tasks);

$holidaysbydate = array();
foreach($holidays as $holiday):
	$startdate = $holiday['Holiday']['start_date'];
	$holidaysbydate[$startdate] = $holiday['Holiday'];
endforeach;
unset($holidays);

/*echo '<pre>';
print_r($taskbyclient);
echo '<pre>';*/
?>


<?php
echo $this->Form->create('Timesheet', array('onsubmit'=>'return validateForm();')); ?>
<table width="100%" border="0" cellspacing="1">
  <tr>
    <td>
    <select name="clients" onchange="javascript:getTasks(this.value)">
        <option value="">Select client</option>
		<?php
        $clientsbyclient = array();
        foreach($clients as $client):?>
            <option value="<?php echo $client['Client']['id']?>"><?php echo $client['Client']['title']?></option>
            <?php $clientsbyclient[$client['Client']['id']][] = $client['Client']; ?>
        <?php endforeach; ?>
        <?php unset($clients); ?>
    </select>
    
    <select name="tasks" id="tasks" onchange="javascript:addTask(this.value)">
        <option>No client selected</option>
    </select>
        
    </td>
    <td></td>
    <td></td>
    <td></td>
    <td width="80"><?php echo $this->Form->end('Save Timesheet'); ?></td>
  </tr>
</table>
<?php
/*echo '<pre>';
	print_r($clientsbyclient);
	echo '</pre>';*/
	?>

<table width="100%" border="0" cellspacing="1" class="table">
	<tr>
    <th>Client</th>
    
	<?php
	$oneday = 60*60*24;
	$days = 6;
	$holidaysthisweek = array();
	for($i = $days; $i>=0; $i--):
		$thisday = $weekending - $i*$oneday;
		if(array_key_exists(date('Y-m-d',$thisday), $holidaysbydate)){
			$holidaysthisweek[date('l', $thisday)] = 'readonly="readonly"';
			$holiday = $holidaysbydate[date('Y-m-d',$thisday)]['title'];
		}else{
			$holidaysthisweek[date('l', $thisday)] = '';
			$holiday = '';
		} ?>
        
        <th class="colum-thin"><?php 
		echo '<p class="holiday">'.$holiday.'</p>';
        echo date('D', $weekending - $i*$oneday);
        echo '<br />';
        echo date('jS',$weekending - $i*$oneday); ?></th>
	<?php endfor;
	$days = array('mon'=>'Monday','tue'=>'Tuesday','wed'=>'Wednesday','thur'=>'Thursday','fri'=>'Friday','sat'=>'Saturday','sun'=>'Sunday'); ?>
    
    <th class="colum-thin">TOTAL</th>
    </tr>
    
    <?php
	$content = json_decode($timesheet['Timesheet']['content']);
	
	foreach($content as $key => $item):
		if($key == 'total'){ ?>
			<tr>
              <td class="total">TOTAL<p></td>
              
              <?php 
			  $totalvalues = $item; 
			  foreach($totalvalues as $key => $totalvalue):?>
              	<td class="total"><input type="text" readonly="readonly" name="data[content][total][<?php echo $key; ?>]" id="total_<?php echo $key; ?>" class="textfieldsmall" value="<?php echo $totalvalue; ?>"></td>
              <?php endforeach; ?>
              <?php unset($totalvalues); ?>
              
            </tr>
		<?php
        }else{ ?>
        	
        <?php
			$tasks = $item;
			/*echo '<pre>';
			//print_r($tasks);
			//print_r($taskbyclient);
			echo '</pre>';*/
			 ?>
			<tr class="column_client_<?php echo $key; ?> client_<?php echo $key; ?>" id="client_<?php echo $key; ?>">
				<td colspan="9" class="client">
                <?php echo $this->Html->image('cross.png', array('alt' =>'delete', 'width' => '10', 'border' => '0', 'name' => 'delete client', 'onclick' => 'javascript:removeClient("'.$key.'","'.$clientsbyclient[$key][0]['title'].'")')); ?>
				<?php echo $clientsbyclient[$key][0]['title']; ?></td>
			</tr>
            
            <?php
			foreach($tasks as $taskkey => $task):?>
            	<tr class="column_client_<?php echo $key; ?> taskof_<?php echo $key; ?>" id="task_<?php echo $taskkey; ?>">
                    
                    <?php
					$counttask = 0;
					 foreach($task as $day => $taskvalue):
						if($day == 'description'){?>
							<td class="task bullet">
                            	<?php 
								echo $this->Html->image('cross.png', array('alt' =>'delete', 'width' => '10', 'border' => '0', 'onclick' => 'javascript:removeTask("'.$key.'","'.$taskkey.'","'.$taskbyclient[$key][$taskkey]['title'].'")')); ?>
								<?php echo $taskbyclient[$key][$taskkey]['title']; ?>
                            	<textarea name="data[content][<?php echo $key; ?>][<?php echo $taskkey; ?>][description]" class="textarea" placeholder="brief description"><?php echo $taskvalue; ?></textarea></td>
                        <?php
						$counttask++;
						}else{
							if($day == 'total'){?>
                    			<td class="total"><input type="text" readonly="readonly" name="data[content][<?php echo $key; ?>][<?php echo $taskkey; ?>][<?php echo $day; ?>]" class="textfieldsmall" id="<?php echo 'client_'.$taskkey.'_'.$day; ?>" value="<?php echo $taskvalue; ?>"></td>
                             <?php
							}else{?>
                            	<td class="hours"><input type="text" <?php echo $holidaysthisweek[$days[$day]]; ?> onchange="calculate(this,'client_<?php echo $taskkey; ?>','<?php echo $day; ?>')" name="data[content][<?php echo $key; ?>][<?php echo $taskkey; ?>][<?php echo $day; ?>]" class="textfieldsmall <?php echo 'client_'.$taskkey; ?> <?php echo $day; ?>" id="<?php echo 'client_'.$taskkey.'_'.$day; ?>" value="<?php echo $taskvalue; ?>"></td>
                    	<?php 
							}
						} 
						?>
                    <?php endforeach; ?>
            		<?php unset($task); ?>
            	</tr>
                
			<?php
			$counttask = 0; 
			endforeach; ?>
            <?php unset($client); ?>
            
        <?php
		}
    endforeach;
	unset($content);
	?>
</table>

<script>
var clients = new Array();
clients = <?php echo json_encode($clientsbyclient); ?>;

var tasks = new Array();
tasks = <?php echo json_encode($taskbyclient); ?>;

var holidaysthisweek = new Array();
holidaysthisweek = <?php echo json_encode($holidaysthisweek); ?>;

</script>
